#include "manager/RulesManager.h"
#include "SdqException.h"

#include <QMap>
#include <QPair>
#include <gtest/gtest.h>

TEST(RulesManager, Easy)
{
   QMap<QPair<quint8, quint8>, quint8> grid;
   grid[qMakePair((quint8) 0, (quint8) 2)] = 4;
   grid[qMakePair((quint8) 0, (quint8) 5)] = 7;
   grid[qMakePair((quint8) 0, (quint8) 6)] = 6;
   grid[qMakePair((quint8) 0, (quint8) 7)] = 3;
   grid[qMakePair((quint8) 1, (quint8) 0)] = 6;
   grid[qMakePair((quint8) 1, (quint8) 4)] = 3;
   grid[qMakePair((quint8) 1, (quint8) 6)] = 9;
   grid[qMakePair((quint8) 1, (quint8) 7)] = 5;
   grid[qMakePair((quint8) 2, (quint8) 0)] = 3;
   grid[qMakePair((quint8) 2, (quint8) 2)] = 9;
   grid[qMakePair((quint8) 2, (quint8) 4)] = 6;
   grid[qMakePair((quint8) 2, (quint8) 5)] = 1;
   grid[qMakePair((quint8) 2, (quint8) 8)] = 7;
   grid[qMakePair((quint8) 3, (quint8) 2)] = 2;
   grid[qMakePair((quint8) 3, (quint8) 3)] = 3;
   grid[qMakePair((quint8) 3, (quint8) 4)] = 8;
   grid[qMakePair((quint8) 3, (quint8) 6)] = 5;
   grid[qMakePair((quint8) 4, (quint8) 0)] = 9;
   grid[qMakePair((quint8) 4, (quint8) 1)] = 4;
   grid[qMakePair((quint8) 4, (quint8) 7)] = 7;
   grid[qMakePair((quint8) 4, (quint8) 8)] = 8;
   grid[qMakePair((quint8) 5, (quint8) 2)] = 5;
   grid[qMakePair((quint8) 5, (quint8) 4)] = 9;
   grid[qMakePair((quint8) 5, (quint8) 5)] = 2;
   grid[qMakePair((quint8) 5, (quint8) 6)] = 1;
   grid[qMakePair((quint8) 6, (quint8) 0)] = 2;
   grid[qMakePair((quint8) 6, (quint8) 3)] = 8;
   grid[qMakePair((quint8) 6, (quint8) 4)] = 4;
   grid[qMakePair((quint8) 6, (quint8) 6)] = 3;
   grid[qMakePair((quint8) 6, (quint8) 8)] = 5;
   grid[qMakePair((quint8) 7, (quint8) 1)] = 9;
   grid[qMakePair((quint8) 7, (quint8) 2)] = 8;
   grid[qMakePair((quint8) 7, (quint8) 4)] = 2;
   grid[qMakePair((quint8) 7, (quint8) 8)] = 4;
   grid[qMakePair((quint8) 8, (quint8) 1)] = 3;
   grid[qMakePair((quint8) 8, (quint8) 2)] = 1;
   grid[qMakePair((quint8) 8, (quint8) 3)] = 6;
   grid[qMakePair((quint8) 8, (quint8) 6)] = 8;
   RulesManager rulesManager;
   ASSERT_NO_THROW(rulesManager.initialize(grid));
   ASSERT_NO_THROW(rulesManager.applyRules());
}

TEST(RulesManager, Demoniac)
{
   QMap<QPair<quint8, quint8>, quint8> grid;
   grid[qMakePair((quint8) 0, (quint8) 2)] = 3;
   grid[qMakePair((quint8) 0, (quint8) 3)] = 7;
   grid[qMakePair((quint8) 1, (quint8) 0)] = 8;
   grid[qMakePair((quint8) 1, (quint8) 4)] = 2;
   grid[qMakePair((quint8) 1, (quint8) 7)] = 4;
   grid[qMakePair((quint8) 1, (quint8) 8)] = 6;
   grid[qMakePair((quint8) 2, (quint8) 0)] = 9;
   grid[qMakePair((quint8) 2, (quint8) 7)] = 7;
   grid[qMakePair((quint8) 3, (quint8) 3)] = 6;
   grid[qMakePair((quint8) 3, (quint8) 6)] = 3;
   grid[qMakePair((quint8) 3, (quint8) 7)] = 2;
   grid[qMakePair((quint8) 4, (quint8) 0)] = 1;
   grid[qMakePair((quint8) 4, (quint8) 2)] = 4;
   grid[qMakePair((quint8) 4, (quint8) 4)] = 9;
   grid[qMakePair((quint8) 4, (quint8) 6)] = 8;
   grid[qMakePair((quint8) 4, (quint8) 8)] = 7;
   grid[qMakePair((quint8) 5, (quint8) 1)] = 8;
   grid[qMakePair((quint8) 5, (quint8) 2)] = 2;
   grid[qMakePair((quint8) 5, (quint8) 5)] = 1;
   grid[qMakePair((quint8) 6, (quint8) 1)] = 2;
   grid[qMakePair((quint8) 6, (quint8) 8)] = 5;
   grid[qMakePair((quint8) 7, (quint8) 0)] = 4;
   grid[qMakePair((quint8) 7, (quint8) 1)] = 5;
   grid[qMakePair((quint8) 7, (quint8) 4)] = 1;
   grid[qMakePair((quint8) 7, (quint8) 8)] = 8;
   grid[qMakePair((quint8) 8, (quint8) 5)] = 9;
   grid[qMakePair((quint8) 8, (quint8) 6)] = 6;
   RulesManager rulesManager;
   ASSERT_NO_THROW(rulesManager.initialize(grid));
   ASSERT_NO_THROW(rulesManager.applyRules());
}

TEST(RulesManager, BadInput)
{
   QMap<QPair<quint8, quint8>, quint8> grid;
   RulesManager rulesManager;
   grid[qMakePair((quint8) 0, (quint8) 0)] = 0;
   ASSERT_THROW(rulesManager.initialize(grid), SdqException);

   grid.clear();
   grid[qMakePair((quint8) 0, (quint8) 0)] = 10;
   ASSERT_THROW(rulesManager.initialize(grid), SdqException);

   grid.clear();
   grid[qMakePair((quint8) 9, (quint8) 0)] = 5;
   ASSERT_THROW(rulesManager.initialize(grid), SdqException);

   grid.clear();
   grid[qMakePair((quint8) 0, (quint8) 9)] = 5;
   ASSERT_THROW(rulesManager.initialize(grid), SdqException);
}
